package com.witframework.web.filter;

import java.io.*;
import java.util.*;

import javax.servlet.http.*;
import com.oreilly.servlet.MultipartRequest;

/** A request wrapper to support MultipartFilter.
 * 该类改编自<code>com.oreilly.servlet.MultipartWrapper</code>
 * The filter capability requires Servlet API 2.3.
 * 
 * @see mana.system.oreilly.MultipartFilter
 * 
 * 
 * @pdOid 9ccff70f-e7ec-40a6-9cf5-fa6bc47d6149 */
public class MultipartWrapper extends HttpServletRequestWrapper {
   /** @pdOid 1f8974b8-2d1c-408d-a5b4-df1d92d3d2af */
   MultipartRequest mreq = null;
   
   /** @param req 
    * @param dir
    * @exception IOException
    * @pdOid f83622ce-bd0b-4b32-a87c-a803d827af06 */
   public MultipartWrapper(HttpServletRequest req, String dir) throws IOException                                    {
     super(req);
     mreq = new MultipartRequest(req, dir);
   }
   
   /** @param req 
    * @param dir 
    * @param maxPostSize 
    * @param encoding
    * @exception IOException
    * @pdOid 35e74f01-e4f9-4568-a1c9-f281e4b46740 */
   public MultipartWrapper(HttpServletRequest req, String dir, int maxPostSize, String encoding) throws IOException                                    {
     super(req);
     mreq = new MultipartRequest(req,dir,maxPostSize,encoding,new com.oreilly.servlet.multipart.DefaultFileRenamePolicy());
   }
   
   /**  Methods to replace HSR methods
    * 
    * @pdOid 2f5a8b0f-d944-4bfe-87fc-16f181b3dea8 */
   public Enumeration<String> getParameterNames() {
       return mreq.getParameterNames();
     }
   
   /** @param name
    * @pdOid 314a4485-4113-405e-8540-cb86a979ae65 */
   public String getParameter(String name) {
       return mreq.getParameter(name);
     }
   
   /** @param name
    * @pdOid 44cffa8b-087b-4199-b9d7-04d0ada3559d */
   public String[] getParameterValues(String name) {
       return mreq.getParameterValues(name);
     }
   
   /** @pdOid 7a0b052b-2fcf-4378-982c-e35d366eaed6 */
   public Map<String, Object> getParameterMap() {
     Map map = new HashMap<String, Object>();
     Enumeration<String> enumeration = getParameterNames();
     while (enumeration.hasMoreElements()) {
       String name = enumeration.nextElement();
       map.put(name, mreq.getParameterValues(name));
     }
     return map;
   }
   
   /**  Methods only in MultipartRequest
    * 
    * @pdOid 0143776c-0a01-4c9f-bfc4-cfbf7c542715 */
   public Enumeration getFileNames() {
     return mreq.getFileNames();
   }
   
   /** @param name
    * @pdOid 01612786-dd30-41e4-a64f-bf14837bc9a7 */
   public String getFilesystemName(String name) {
     return mreq.getFilesystemName(name);
   }
   
   /** @param name
    * @pdOid cff65251-7469-4ce9-9285-52c11346aaf0 */
   public String getContentType(String name) {
     return mreq.getContentType(name);
   }
   
   /** @param name
    * @pdOid fc412294-9e5a-4595-8ce8-4366ff9f0e0d */
   public File getFile(String name) {
     return mreq.getFile(name);
   }
   
   /** @param name
    * @pdOid 900377bb-e293-490f-961b-6fa6ea6fe388 */
   public String getOriginalFileName(String name) {
     return mreq.getOriginalFileName(name);
   }
   public void finalize(){
		try {
			super.finalize();
		} catch (Throwable e) {		
		}
		Enumeration<String> fileNames = getFileNames();
	   	while (fileNames.hasMoreElements()) {
	   		String name = (String) fileNames.nextElement();
	   		File file=getFile(name);
	   		if(file!=null)
	   			file.delete();   		
	   }
   }
}